#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int n,q,c,a[N],f[N];
namespace sub1{
	int cnt,head[N],to[N<<1],nxt[N<<1];
	inline void add(int x,int y)
	{
		nxt[++cnt] = head[x];
		head[x] = cnt;
		to[cnt] = y;
	}
	int ans[N],sum[N],ff[N]; 
	vector<pair<int,int>> vec[1<<15];
	inline void main()
	{
		for(int i = 2;i<=n;i++)
			add(f[i],i);
		for(int i = 1,x,z;i<=q;i++) cin>>z>>x,vec[z].push_back({x,i});
		for(int z = 1;z<(1<<c);z++)
		{
			if(!vec[z].size()) continue;
			ff[1] = sum[1] = 1;
//			for(int i = 1;i<=n;i++) sum[i] = 1,ff[i] = i; 
			for(int i = 2;i<=n;i++)
			{
				if((a[f[i]]|a[i]|z)==z) ff[i] = ff[f[i]],sum[ff[i]]++;
				else sum[i] = 1,ff[i] = i;
			}
			for(auto _:vec[z])
				ans[_.second] = sum[ff[_.first]];
		}
		for(int i = 1;i<=q;i++) cout<<max(ans[i],1)<<'\n';
	}
}
namespace sub2{
	int st[N][20];
	int ask(int l,int r)
	{
		int lg = __lg(r-l+1);
		return (st[l][lg]|st[r-(1<<lg)+1][lg]);
	}
	inline void main()
	{
		for(int i = 1;i<=n;i++) st[i][0] = a[i];
		for(int j = 1;j<20;j++)
			for(int i = 1;i+(1<<j)-1<=n;i++)
				st[i][j] = (st[i][j-1]|st[i+(1<<j-1)][j-1]);
		while(q--)
		{
			int z,x;
			cin>>z>>x;
			if((a[x]|z)!=z)
			{
				cout<<"1\n";
				continue;
			}
			int ans = 1;
			int l = 0,r = x-1,res = 0;
			while(l<=r)
			{
				int mid = (l+r)/2;
				if((ask(x-mid,x)|z)==z) res = mid,l = mid+1;
				else r = mid-1;
			}
			ans+=res;
			l = 0,r = n-x,res = 0;
			while(l<=r)
			{
				int mid = (l+r)/2;
				if((ask(x,x+mid)|z)==z) res = mid,l = mid+1;
				else r = mid-1;
			}
			ans+=res;
			cout<<ans<<'\n';
		}
	}
}
signed main()
{
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>q>>c;
	for(int i = 1;i<=n;i++)
		cin>>a[i],a[i] = (1<<a[i]);
	bool fl = 1;
	for(int i = 2;i<=n;i++) cin>>f[i],fl&=(f[i]==i-1);
	if(fl) sub2::main();
	else sub1::main();
	return 0;
}

